package com.song.tools.generator;

import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import com.song.tools.generator.config.GeneratorConfig;
import com.song.tools.generator.config.MysqlConnectionConfig;
import com.song.tools.generator.util.PropertiesUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 生成器client-入口
 *
 * @author song tools
 * @since 2024-06-24
 */
public class GeneratorClient {

    private static final String DB_URL = PropertiesUtil.MysqlProperties.URL;
    private static final String DB_USERNAME = PropertiesUtil.MysqlProperties.USERNAME;
    private static final String DB_PASSWORD = PropertiesUtil.MysqlProperties.PASSWORD;

    private static final String TABLE_NAME_PREFIX = PropertiesUtil.GeneratorConfigProperties.TABLE_NAME_PREFIX;


    public static void main(String[] args) {
        // 获取当前项目对应的目录
        String userDir = System.getProperty("user.dir");

        // 数据库连接信息配置
        MysqlConnectionConfig mysqlConnectionConfig = new MysqlConnectionConfig(DB_URL, DB_USERNAME, DB_PASSWORD);

        List<String> tablePrefixList = new ArrayList<>();
        if (StrUtil.isNotBlank(TABLE_NAME_PREFIX)) {
            tablePrefixList.addAll(Arrays.asList(TABLE_NAME_PREFIX.split(StrPool.COMMA)));
        }

        // 生成器的通用配置信息
        GeneratorConfig generatorConfig = new GeneratorConfig()
                .setMysqlConnectionConfig(mysqlConnectionConfig)
                .setAuthor(PropertiesUtil.GeneratorConfigProperties.AUTHOR)
                .setEnableSpringDoc(true)
                .setProjectDir(userDir + "/" + PropertiesUtil.GeneratorConfigProperties.PROJECT_DIR + "/")
                .setParent(PropertiesUtil.GeneratorConfigProperties.PARENT_PACKAGE)
                .setLogicDeleteColumnName(PropertiesUtil.GeneratorConfigProperties.DELETE_COLUMN_NAME)
                .setTablePrefixList(tablePrefixList)
                .setTemplate("mybatis-templates");

        // 生成代码
        generateCode(generatorConfig);
    }


    private static void generateCode(GeneratorConfig generatorConfig) {
        CodeGenerator codeGenerator = new CodeGenerator();

        // 生成字典表代码
        codeGenerator.execute(generatorConfig.toBuilder()
                // 表名
                .tableName("pine_dictionary")
                // 业务模块名，父级包下面的一个包名
                .moduleName("songtest")
                // 主键类型
                .primaryKeyType("Long")
                // 主键名
                .primaryKeyName("id")
                // 是否实现poi功能（目前支持excel导入导出，以及获取模版）
                .poiServiceImpl(true)
                // 枚举类配置,配置字段名和类名
                .enumNameMap(Map.ofEntries(Map.entry("status", "StatusEnum")))
                .build());
    }
}
